package com.baiyang.server.control.util;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Date;

import com.alibaba.fastjson.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.baiyang.server.annotation.Jurisdiction;
import com.baiyang.server.tools.DataTool;
import com.baiyang.server.tools.Letter;


/**
 * @apiNote 文件处理工具类
 * @author XVX
 * @since 2021/11/08
 */

@RestController
@RequestMapping(value = "/fileUtils")
@Jurisdiction(value = "图片操作")
public class FileUtilsCon {
    /**上传地址*/
    @Value("${file.upload.path}")
    private String filePath;
    private Logger log = LoggerFactory.getLogger(this.getClass());
    
    /**
     * @apiNote form-data格式上传文件  
     * @param multipartFile
     * @return 
     * @author XVX
     * @since 2021/11/08
     */
    @RequestMapping(value = "/upload")
	public Letter<JSONObject> upload(@RequestParam("file") MultipartFile multipartFile){
		Letter<JSONObject> letter = new Letter<JSONObject>();
        if (!multipartFile.isEmpty()) {    
            try {       
            	String path = DataTool.sdf_ymd.format(new Date());
            	File file = new File(filePath + File.separator + path);
            	if(!file.exists()) {
            		file.mkdirs();
            	}
            	path += File.separator + DataTool.createUUID() + getFileExtension(multipartFile);
                BufferedOutputStream out = new BufferedOutputStream(    
                        new FileOutputStream(new File(
                                filePath + File.separator + path)));
                out.write(multipartFile.getBytes());    
                out.flush();    
                out.close();    
                log.info("文件上传成功" + path);
                letter.setResult("SUCCESS");
                letter.setDetail(path);
                JSONObject object = new JSONObject();
                object.put("type",multipartFile.getContentType());
                object.put("path",path);
                letter.setData(object);
            } catch (FileNotFoundException e) {    
                e.printStackTrace();    
                letter.setResult("ERROR");
            } catch (IOException e) {    
                e.printStackTrace();    
                letter.setResult("ERROR");
            }    
        } else {    
            letter.setResult("FAIL");
            letter.setDetail("文件为空");
        }  
        return letter;
	}

    /**
     * @apiNote 获取文件后缀名
     * @param cFile
     * @return 后缀名
     * @author XVX
     * @since 2021/11/18
     */
	private String getFileExtension(MultipartFile cFile) {
		String originalFileName = cFile.getOriginalFilename();
		return originalFileName.substring(originalFileName.lastIndexOf("."));
	}
}
